package com.sl.au.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.sl.au.entity.ReplaceDevicePrice;
import com.sl.au.entity.ReplacementDevice;
import com.sl.au.service.ReplaceDevicePriceService;
import com.sl.au.service.ReplacementDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sl.au.entity.Account;
import com.sl.au.entity.Module;
import com.sl.au.service.AttachmentService;

@RestController
@RequestMapping(value = "/ui/priceBook/replaceDevice")
public class ReplaceDevicePriceController extends BaseController {

    @Autowired
    ReplaceDevicePriceService replaceDeviceService;
    
    @Autowired
    ReplacementDeviceService replacementDeviceService;

    @Autowired
    AttachmentService attachmentService;


    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = createLayoutView("priceBook/replaceDevice/replaceDevicePriceIndex");
        Account a = this.getMyAccount();
        String superior=a.getUser().getSaleArea().getSuperior();
        modelAndView.addObject("superior", superior);
        String name="replaceDevice"; 
        Module module = this.getPageModule(name);
        modelAndView.addObject("module", module);
        return modelAndView;
    }
    @RequestMapping(value = "getpage", method = RequestMethod.GET)
    public Map<String, Object> getPage(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        String key=request.getParameter("key");
        String superior=request.getParameter("superior");
        Account a = this.getMyAccount();
        superior = a.getUser().getSaleArea().getSuperior();
        String name="replaceDevice"; 
        Module module = this.getPageModule(name);
        String dataFilter=module.getDataFilter();
        List<ReplaceDevicePrice> ReplaceDevices=null;
        ReplaceDevices = replaceDeviceService.getReplaceDevices(start, length, key, superior,dataFilter,null, null);
        long count =0;
        if(dataFilter.equals("所有")){
        	count = replaceDeviceService.getRepository().getCount(key);
        }else if(dataFilter.equals("本部门")){
        	count = replaceDeviceService.getRepository().getCount(superior, key); 
        }

        map.put("data", ReplaceDevices);
        map.put("draw", request.getParameter("draw"));
        map.put("recordsFiltered", count);
        map.put("recordsTotal", count);
        return map;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Map<String, Object> saveReplaceDevicePrice(HttpServletRequest request, @RequestBody ReplaceDevicePrice price) {
        Map<String, Object> map = new HashMap<String, Object>();  
        String replaceId=price.getReplacementDevice().getId();
        ReplacementDevice replace=replacementDeviceService.getRepository().findOne(replaceId);
        price.setReDeviceName(replace.getDeviceName());
        price.setReplacementDevice(replace);
        replaceDeviceService.getRepository().save(price);
        map.put("code", 0);
        return map;
    }
    /**
     * EXCEL导入处理 *
     */
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public Map<String,Object>  importExcel(@RequestParam("file") MultipartFile file,
                                           HttpServletRequest request,			RedirectAttributes redirectAttributes) {

        Map<String,Object> map=new HashMap<String,Object>();
        int code=-1;
        long count=-1;
        Account a = this.getMyAccount();
        String superior=a.getUser().getSaleArea().getSuperior();
        String saveFileName=attachmentService.upload(file);
        //文件传后，再读取EXCEL写入数据库
        if(StringUtils.hasText(saveFileName)){
            count=replaceDeviceService.importExcel(saveFileName, superior);
            code=0;
        }
        map.put("code",code);
        map.put("count",count);
        return  map;
    }

    
}
